<?php

namespace Admin\Controller;
use Think\Controller;
use Think\Db;
class ConsoleController extends Controller {
	
	public function index(){
			$map['time'] = array(array('egt',strtotime(date('Y-m-d'))),array('lt',strtotime(date('Y-m-d'))+86400),'and');
	    	$map['status'] = 1;
	    
	
		   $list = 	M('user_yuebao')->where($map)->select();
		  
		  if($list){
		  	
		  	foreach ($list as $v){
		  		 	Db::startTrans();
				 	 $moneys = M('user_yuebao')->where("id = {$v['id']}")->find();
		
				 	 if($moneys['status']==1){
				 	 	  $res1 = M('user')->where("id = {$v['user_id']}")->setInc ('points',$v['money']);
					 	  $res2 =  M('user')->where("id = {$v['user_id']}")->setDec('yuebao',$v['money']);
					      $res3 =  M('user_yuebao')->where("id = {$v['id']}")->save(['status'=>3]);
					      $res4 =  M('user')->where("id = {$v['user_id']}")->setInc('totalbalance',$v['fee']);
					      $res5 =  M('user')->where("id = {$v['user_id']}")->setInc('totaybalance',$v['fee']); 
					      		 
					 	if($res1&& $res2 && $res3 && $res4 && $res5){
					 		 Db::commit();
					 	}else{
					 		Db::rollback();
					 		  
					 	}
				 	 }else{
				 	 		Db::rollback();
				 		 
				 	 }
		  	}
		  }
	}
	public function add(){
	  $list = 	 M('user')->where("totaybalance > 0")->select();
	    if($list){
		  	
		  	foreach ($list as $v){
		  		Db::startTrans();
		  	   $res4 =  M('user')->where("id = {$v['id']}")->save(['totaybalance'=>0]);
				$res5 =  M('user')->where("id = {$v['id']}")->setInc('yesterdaybalance',$v['totaybalance']); 
			 	if($res4 && $res5){
			 		 Db::commit();
			 	
			 	}else{
			 		Db::rollback();
			 
			 	}
		  	}
		  	
	    }
	  
	}
	
	public function test() {
		M()->execute("update `think_user` set yuebao=0, yuebao_income=0;");
		M()->execute("TRUNCATE `think_user_yuebao`;");
		
		die();
		$d = strtotime('today -1 day');
		$d0 = strtotime('-7 days', $d);
		$sql = "update think_user_yuebao set `created_at`={$d0}, `time`={$d} where name='test01'";
		$r = M()->execute($sql);
		var_dump([$r, $sql]);die;
		// var_dump(date('Y-m-d H:i:s', $d));die;
		// $creat = strtotime()
		// M()->execute("update think_user_yuebao set created_at")
	}
	public function indext(){
			$dq_time=date("Ymd",time());
	    	$map['status'] = 1;
	    
	
		   $list = 	M('user_yuebao')->where($map)->select();
		  
		  if($list){
		  	
		  	foreach ($list as $v){
		  		     
				 	 $check_user = M('user')->where("id = {$v['user_id']}")->find();
		             if($check_user){
		             	$check_type = M('user_yuetype')->where("id = {$v['typeid']}")->find();
		             	$shouyi=$v['rsy'];//每天收益
		             	$userid=$v['user_id'];
		             	$money=$v['money'];//本金
		             	if(($v['sfday']+1)>=$v['days']){
		             		$sql = "update think_user set points = points+{$money}+{$shouyi},yuebao_income=yuebao_income+{$shouyi}, yuebao=yuebao-{$money} where id={$userid}";
		             	M()->execute($sql);
		             	$sql = "update think_user_yuebao set status=2,sfday=sfday+1 where id={$v['id']}";
		             	M()->execute($sql);	
		             	echo '释放成功';
		             	}else{
		             	$sql = "update think_user set points = points+{$shouyi},yuebao_income=yuebao_income+{$shouyi} where id={$userid}";
		             	
		             	M()->execute($sql);	
		             	
		             	$sql = "update think_user_yuebao set sfday=sfday+1 where id={$v['id']}";
		             	M()->execute($sql);	
		             	echo '释放成功';
		             	}
		             	
		             }
				 	 
		  	}
		  }
	}
	// 每日零点以后
	public function yeb_cash(){
		
		/*
		$r = M()->execute("update think_user_yuetype set name='bcd' where id=15");
		var_dump($r);die;
		*/
		
		$today = strtotime('today');
		// $today = strtotime('today +2 days');
		
		// 结帐时间小于今天0点的，status=1的，各用户统计并分组，一次给清
		$sql = "select sum(fee) as money, sum(money) as zhuancu,  count(user_id) as total, user_id from think_user_yuebao where status=1  group by user_id";
		$list = M()->query($sql);
		if(!$list){
			return ;
		}
		
		var_dump($list);
		foreach($list as $rs){
			$money = $rs['money'];	// 统计后的收益
			$total = intval($rs['total']);	// 参与统计的投资表(`user_yuebao`) 行数, 批量更新
			$userid = $rs['user_id'];
			$zhuancu = $rs['zhuancu']; // 各项投资需要转出的总和(结帐)
			Db::startTrans();
			
			try {
				// 更新收益 user 表. 
				// 如果user.yuebao统计有问题，应该实时统计 user_yuebao where status=1)
				$sql = "update think_user set points = points+{$money}+{$zhuancu}, yuebao=yuebao-{$zhuancu},yuebao_income=yuebao_income+{$money} where id={$userid}";
				if(!M()->execute($sql)){
					throw new \Exception($sql);
				}
				
				// 批量更新投资表(user_yuebao) 的各个状态， 设置已完结
				$sql = "update think_user_yuebao set status=2 where status=1 and `time` < {$today} and user_id={$userid}";
				
				if($total !== M()->execute($sql)){
					throw new \Exception($sql);
				}
				DB::commit();
				
			} catch(\Exception $e){
				echo "userid: {$userid}, 发放失败, 信息：".$e->getMessage().'<br/>'.PHP_EOL;
				Db::rollback();
				break;
			}
		}
	}


}
